/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			Flt_Words.cc
  \date			Aug 2011
  \author		TNick

  \brief		Contains the implementation for Flt_Words class


 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @n
 Please read ReadMe.txt and Licence.txt in root folder @n
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @n

*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include    <QWidget>
#include    <QGroupBox>
#include    <QSettings>
#include    <QVBoxLayout>
#include    <QPlainTextEdit>
#include    <QRegExp>

#include    <data_store/pagedata.h>

#include	"flt_words.h"



/*  INCLUDES    ============================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */

class       Flt_Words_Data    {
public:
	QVBoxLayout *verticalLayout;

	QGroupBox * grp_name;
	QGroupBox * grp_cont;

	QPlainTextEdit * tx_name;
	QPlainTextEdit * tx_cont;

};



// the unique instance
Flt_Words *       Flt_Words::uniq_instance = NULL;


/* ------------------------------------------------------------------------- */
Flt_Words::Flt_Words	(void)
{

	QSettings settings;
	settings.beginGroup("Flt_Words");

	flags = (FLG)
			settings.value(QString("flags"),
						   (NAME_ENABLED |
							FILTER_ENABLED
							)).toInt();

    lst_name =
            settings.value(QString("lst_name"),
						   (
                               "Untitled"))
            .toString()
            .split("\n",
                   QString::SkipEmptyParts);
    lst_cont =
            settings.value(QString("lst_cont"),
                           (
                               "Mougly"))
            .toString()
            .split("\n",
                   QString::SkipEmptyParts);

	settings.endGroup();


}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
Flt_Words::~Flt_Words	(void)
{

	QSettings settings;
	settings.beginGroup("Flt_Words");

	settings.setValue(QString("flags"), flags);
    settings.setValue(QString("lst_name"), lst_name.join("\n"));
    settings.setValue(QString("lst_cont"), lst_cont.join("\n"));

	settings.endGroup();
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
Flt_Words* Flt_Words::initFilter                (void)
{
	if (uniq_instance == NULL)
		{
			uniq_instance = new Flt_Words();
			registerFilter(uniq_instance);
		}
	return uniq_instance;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QString     Flt_Words::name                       (void)
{
	return QString("Words");
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool        Flt_Words::active                     (void)
{
	return filtActive();
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void *      Flt_Words::createOptions              (QWidget* parent)
{
	Flt_Words_Data * p_data = new Flt_Words_Data();

	p_data->verticalLayout = new QVBoxLayout(parent);

	p_data->grp_name = new QGroupBox(QString("Filter by name"));
	p_data->grp_name->setCheckable(true);
	p_data->grp_name->setChecked(nameEnabled());
	p_data->verticalLayout->addWidget(p_data->grp_name);
	QVBoxLayout * v1 = new QVBoxLayout(p_data->grp_name);
	p_data->grp_name->setLayout(v1);
    p_data->tx_name = new QPlainTextEdit(lst_name.join("\n"));
	v1->addWidget(p_data->tx_name);
	p_data->grp_name->show();

	p_data->grp_cont = new QGroupBox(QString("Filter by content"));
	p_data->verticalLayout->addWidget(p_data->grp_cont);
	p_data->grp_cont->setCheckable(true);
	p_data->grp_cont->setChecked(contEnabled());
	QVBoxLayout * v2 = new QVBoxLayout(p_data->grp_cont);
	p_data->grp_cont->setLayout(v2);
    p_data->tx_cont = new QPlainTextEdit(lst_cont.join("\n"));
	v2->addWidget(p_data->tx_cont);
	p_data->grp_cont->show();

	parent->setLayout(p_data->verticalLayout);

	return p_data;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void        Flt_Words::saveOptions                (
	QWidget* parent, bool active_sts, void* data)
{
	Q_UNUSED(parent);

	Flt_Words_Data * p_data = (Flt_Words_Data *) data;

	setNameEnabled(p_data->grp_name->isChecked());
	setContEnabled(p_data->grp_cont->isChecked());

    lst_name = p_data->tx_name->toPlainText()
            .split("\n",
                   QString::SkipEmptyParts);
    for (int i = 0; i < lst_name.count(); i++)
        {
            lst_name[i] = lst_name[i].trimmed();
        }

    lst_cont = p_data->tx_cont->toPlainText()
            .split("\n",
                   QString::SkipEmptyParts);
    for (int i = 0; i < lst_cont.count(); i++)
        {
            lst_cont[i] = lst_cont[i].trimmed();
        }

	setFiltActive(active_sts);

	delete p_data;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool        Flt_Words::passesFilter               (PageData * pg_data)
{

	if (pg_data == NULL) return false;


	/* check the title for a match */
	if (nameEnabled())
        {
            foreach (QString s_itr, lst_name)
                {
                    if (pg_data->displName().contains(s_itr, Qt::CaseInsensitive))
                        {
                            return true;
                        }
                }
		}

	/* check the content for a match */
	if (contEnabled())
		{
            foreach (QString s_itr, lst_cont)
                {
                    if (pg_data->displName().contains(s_itr, Qt::CaseInsensitive))
                        {
                            return true;
                        }
                }
        }

	return false;
}
/* ========================================================================= */


/*  CLASS    =============================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
